// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require("gulp")
const pluginsLoader = require("gulp-load-plugins")
const config = require("./gulp.config.js")
const del = require("del")
const browserSync = require('browser-sync')
const bs = browserSync.create()
// 自动加载所有已经安装的插件
const plugins = pluginsLoader()

const clean = () => {
    return del([config.path.dist, config.path.temp])
}

const style = () => {
    return src(config.path.styles, { cwd: config.path.src, base: config.path.src })
        .pipe(plugins.sass())
        .pipe(dest(config.path.temp))
        .pipe(bs.reload({ stream: true }))
}

const script = () => {
    return src(config.path.scripts, { cwd: config.path.src, base: config.path.src })
        .pipe(plugins.babel({
            // 指定具体的转换工具
            presets: ['@babel/preset-env']
        }))
        .pipe(dest(config.path.temp))
        .pipe(bs.reload({ stream: true }))
}

const page = () => {
    return src(config.path.html, { cwd: config.path.src, base: config.path.src })
        .pipe(plugins.swig({}))
        .pipe(dest(config.path.temp))
        .pipe(bs.reload({ stream: true }))
}

const extra = () => {
    return src("**", { cwd: config.path.public, base: config.path.public })
        .pipe(dest(config.path.dist))
}

const image = () => {
    return src(config.path.images, { cwd: config.path.src, base: config.path.src })
        .pipe(plugins.imagemin())
        .pipe(dest(config.path.dist))
}

const font = () => {
    return src(config.path.fonts, { cwd: config.path.src, base: config.path.src })
        .pipe(plugins.imagemin())
        .pipe(dest(config.path.dist))
}

const openServe = () => {
    watch(config.path.styles, { cwd: config.path.src }, style)
    watch(config.path.scripts, { cwd: config.path.src }, script)
    watch(config.path.html, { cwd: config.path.src }, page)

    watch(config.path.images, { cwd: config.path.src }, bs.reload)
    watch(config.path.fonts, { cwd: config.path.src }, bs.reload)
    watch(config.path.public, { cwd: config.path.public }, bs.reload)
    bs.init({
        notify: false,
        port: 8080,
        open: true,
        server: {
            baseDir: [config.path.temp, config.path.src, config.path.public],
            routes: {
                '/node_modules': 'node_modules'
            }
        }
    })
}

const useref = () => {
    return src(config.path.tempHtml, { base: config.path.temp })
        .pipe(plugins.useref({
            // 指定资源文件查找路径
            searchPath: [config.path.temp, '.']
        }))
        .pipe(plugins.if(/\.js$/, plugins.uglify()))
        .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
        .pipe(plugins.if(/\.html$/, plugins.htmlmin(
            {
                collapseWhitespace: true,
                minifyCSS: true,
                minifyJS: true
            }
        )))
        .pipe(dest(config.path.dist))
}

const compile = parallel(style, script, page)
const build = series(clean, parallel(series(compile, useref), extra, font))
const serve = series(compile, openServe)

module.exports = {
    clean,
    build,
    serve
}
